package com.jinsi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.*;
import com.jinsi.pojo.topo.ScStandardInfo;
import com.jinsi.service.ScStandardService;
import com.jinsi.service.StuOperationLogService;
import com.jinsi.utils.MapReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Date : 2022/6/29 17:15
 * 小南不难
 **/
@RestController
@RequestMapping("/standard")
public class ScStandardController {
    //定义一个全局返回值
    Map<String, Object> ret = null;

    @Autowired
    private ScStandardService scStandardService;

    @Autowired
    private StuOperationLogService stuOperationLogService;
    //    根据学生信息查询学生标准
    @GetMapping("/getStantardByInfo")
    public Map<String, Object> getStantardByInfo(Integer page,Integer limit,String info) {
        PageHelper.startPage(page,limit);
        List<ScStudentStandard> scStandardBySid = scStandardService.getScStandardBySid2(info);
        PageInfo<ScStandardController> list = new PageInfo(scStandardBySid);
        if (scStandardBySid == null) {
            //集合为空系统为访问到数据库数据
            return MapReturn.getError(null);
        } else {
            //查询成功返回查询数据
            return  MapReturn.getTable("0","成功",(int)list.getTotal(),list.getList());
        }
    }
    //查询欠费信息
    @GetMapping("/getStantardArrearageByInfo")
    public Map<String, Object> getStantardArrearageByInfo(Integer page,Integer limit,String info) {
        PageHelper.startPage(page,limit);
        System.out.println("打印一下传入数据"+ info);
        List<ScStudentStandard> scStandardBySid = scStandardService.getScStandardBySid(info,true);
        if (scStandardBySid == null) {
            //集合为空系统为访问到数据库数据
            return MapReturn.getError(null);
        } else {
            PageInfo<ScStandardController> list = new PageInfo(scStandardBySid);
            //查询成功返回查询数据
            return  MapReturn.getTable("0","成功",(int)list.getTotal(),list.getList());
        }
    }

    //根据id号查询缴费信息
    @GetMapping("/getStantardArrearageById")
    public Map<String, Object> getStantardArrearageById(Integer page,Integer limit,String idList) {
        PageHelper.startPage(page,limit);
        List<ScStudentStandard> scStandardBySid = scStandardService.getScStandardBySid(JSONArray.parseArray(idList));
        if (scStandardBySid == null) {
            //集合为空系统为访问到数据库数据
            return MapReturn.getError(null);
        } else {
            PageInfo<ScStandardController> list = new PageInfo(scStandardBySid);
            //查询成功返回查询数据
            return  MapReturn.getTable("0","成功",(int)list.getTotal(),list.getList());
        }
    }
    //根据标准id号调整标准缓交
    @GetMapping("/updateStandardById")
    public Map<String,Object> updateStandardById(int id, BigDecimal preferentialPrice, String remark,String studentId,String studentName){
        ScStudentOperationLog scStudentOperationLog = new ScStudentOperationLog();
        scStudentOperationLog.setStudentName(studentName);
        scStudentOperationLog.setStudentId(studentId);
        scStudentOperationLog.setOperationDateTime(new Date());
        scStudentOperationLog.setOperationRemark("修改学生订单信息");
        scStudentOperationLog.setStatus(1);
        try {
            stuOperationLogService.addLog(scStudentOperationLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return    scStandardService.updateStandardById(id, preferentialPrice, remark);
    }

    //    学生信息查询(姓名，班级。年份，学期)
    @GetMapping("/getStuInfo")
    public Map<String, Object> getStudentInfo(String info) {
        ScStandardInfo studentInfo = scStandardService.getStudentInfo(info);
        ret = new HashMap<>();
        if (studentInfo == null) {
            //集合为空系统为访问到数据库数据
            ret = MapReturn.getError(null);
        } else {
            //查询成功返回查询数据
            ret = MapReturn.getSuccess(studentInfo);
        }
        return ret;
    }

    //对项目信息进行查询
    @GetMapping("/projectList")
    public Map<String, Object> getProjectList() {
        List<ScProjectList> projectList = scStandardService.getProjectList();
        ret = new HashMap<>();
        if (projectList == null) {
            //集合为空系统为访问到数据库数据
            ret = MapReturn.getError(null);
        } else {
            //查询成功返回查询数据
            ret = MapReturn.getSuccess(projectList);
        }
        return ret;
    }

    //添加人员标准
    @GetMapping("/iptProjectList")
    public Map<String, Object> iptProjectList(String project) {
        System.out.println(project);
        ret = new HashMap<>();
        JSONArray newlist = JSON.parseArray(project);
        System.out.println(newlist);
        ScStudentOperationLog scStudentOperationLog = new ScStudentOperationLog();
        try {
        for (Object o : newlist){
            Map<String,Object> map = (Map<String, Object>) o;
            scStudentOperationLog.setStudentName((String)map.get("studentName"));
            scStudentOperationLog.setStudentId((String)map.get("sid"));
            scStudentOperationLog.setOperationDateTime(new Date());
            scStudentOperationLog.setOperationRemark("添加学生订单信息");
            scStudentOperationLog.setStatus(1);
            stuOperationLogService.addLog(scStudentOperationLog);
            break;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            }
        //将数据添加到数据库
        Integer i = scStandardService.iptProject(newlist);
        if (i != 0) {
            ret = MapReturn.getSuccess(i);
        } else {
            ret = MapReturn.getError(i);
        }
        return ret;
    }
    //自定义添加项目标准


    //项目标准修改
    @GetMapping("/uptProject")
    public Map<String, Object> uptProject(ScStudentStandard studentStandard,String studentName) {
        System.out.println(studentStandard);
        ScStudentOperationLog scStudentOperationLog = new ScStudentOperationLog();
        scStudentOperationLog.setStudentName(studentName);
        scStudentOperationLog.setStudentId(studentStandard.getSid());
        scStudentOperationLog.setOperationDateTime(new Date());
        scStudentOperationLog.setOperationRemark("修改学生订单信息");
        scStudentOperationLog.setStatus(1);
        try {
            stuOperationLogService.addLog(scStudentOperationLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ret = new HashMap<>();
        Integer i = scStandardService.uptProject(studentStandard);
        if (i == 1) {
            //修改成功
            ret = MapReturn.getSuccess(i);
        } else {
            //修改失败
            ret = MapReturn.getError(i);
        }
        return ret;
    }

    //删除项目标准
    @GetMapping("/delProject")
    public Map<String, Object> delProject(int id) {
        ret = new HashMap<>();
        Integer i = scStandardService.delProject(id);
        if (i == 1) {
            //删除成功
            ret = MapReturn.getSuccess(i);
        } else {
            //删除失败
            ret = MapReturn.getError(i);
        }
        return ret;
    }

    //查询宿舍基础标准
    @GetMapping("/getDormitory")
    public Map<String, Object> getDormitory(int inherit) {
        List<ScRates> dormitory = scStandardService.getDormitory(inherit);
        if (dormitory == null) {
            //查询数据出错
            return MapReturn.getError(null);
        }
        //查询成功
        return MapReturn.getCustom("0", "成功", dormitory);
    }

    //新增基础项目收费
    @GetMapping("/iptRates")
    public Map<String, Object> iptRates(ScRates scRates) {
        Integer i = scStandardService.iptRates(scRates);
        if (i == 1) {
            //添加成功
            return MapReturn.getSuccess(i);
        } else {
            //提那家失败
            return MapReturn.getError(i);
        }
    }

    //基础项目收费删除
    @GetMapping("/delRates")
    public Map<String, Object> delRates(int id) {
        Integer i = scStandardService.delRates(id);
        if (i == 1) {
            //添加成功
            return MapReturn.getSuccess(i);
        } else {
            //删除失败
            return MapReturn.getError(i);
        }
    }

    //修改基础项目配置
    @GetMapping("/uptRates")
    public Map<String, Object> uptRates(ScRates scRates) {
        Integer i = scStandardService.uptRates(scRates);
        if (i == 1) {
            //修改成功
            return MapReturn.getSuccess(i);
        } else {
            //修改失败
            return MapReturn.getError(i);
        }
    }

    //查询班级收费标准
    @GetMapping("/getClassesStandard")
    public Map<String, Object> getClassesStandard(int page, int limit, String pastDue,String startTime) {
        PageHelper.startPage(page, limit);
        List<ScClassesStandard> classesStandard = scStandardService.getClassesStandard(pastDue,startTime);
        if (classesStandard == null) {
            //数据库获取数据出错
            return MapReturn.getTable("500", "出了点小毛病。。。", 0, null);

        } else {
            PageInfo<ScClassesStandard> list = new PageInfo(classesStandard);
            return MapReturn.getTable("0", "成功", (int) list.getTotal(), list.getList());
        }
    }

    //学费信息查询
    @GetMapping("/queryClassesStandrad")
    public Map<String,Object> queryClassesStandrad(String pastDue,String startTime){
        System.out.println(pastDue == "");
        System.out.println(startTime);
        return null;
    }


    //修改班级基本消费标准
    @GetMapping("/uptClassesMoney")
    public Map<String, Object> uptClassesMoney(ScClassesStandard scClassesStandard) {
        return scStandardService.uptClassesMoney(scClassesStandard);
    }

    //添加学费期数标准
    @GetMapping("/istClassesPrice")
    public Map<String, Object> istClassesPrice(ScClassesStandard scClassesStandard) {
        return scStandardService.istClassesPrice(scClassesStandard);
    }

    //学费是否启用设置
    @GetMapping("/uptClassesStartUseing")
    public Map<String, Object> uptClassesStartUseing(ScClassesStandard scClassesStandard) {
        return scStandardService.uptClassesStartUseing(scClassesStandard);
    }

    //删除学费收费项
    @GetMapping("/delClassesStandard")
    public Map<String,Object> delClassesStandard(int id){
        return scStandardService.delClasserStandard(id);
    }

    //日志查询操作
    @GetMapping("/delStandardLog")
    public Map<String,Object> delStandradLog(int id){
        return scStandardService.delStandardLog(id);
    }

    //日志查询操作
    @GetMapping("/getStandardLog")
    public Map<String,Object> getStandardLog(int page,int limit){
        PageHelper.startPage(page,limit);
        Map<String, Object> stringObjectMap = scStandardService.getStandardLog();
        if (stringObjectMap.get("state").equals("200")){
            List<ScStandardLog> scStandardLog = (List<ScStandardLog>) stringObjectMap.get("data");
            PageInfo<ScStandardLog> info = new PageInfo(scStandardLog);
            return MapReturn.getTable("0","成功",(int)info.getTotal(),info.getList());
        }
        return stringObjectMap;
    }
}
